//初始化页面
function init() {
    var json=JSON.parse(window.localStorage.getItem("json"));
    var data={"tid":json.tid,"page":1}

    //更新table
    flushTable("/home/selectProfileMsg",JSON.stringify(data),"#page","#profile-container");

    //初始化select表
    getCity($("#city"));
    getLanguage($("#language"));

    //初始化搜索栏
    for(var i=0;i<json.cid.length;i++){
        $("#city").find("option[value="+json.cid[i]+"]").attr("selected",true);
    }
    for(var i=0;i<json.lid.length;i++){
        $("#language").find("option[value="+json.lid[i]+"]").attr("selected",true);
    }

    //如果没有搜索到任何内容则打印相关信息
    if($(".profile").length==0){
        var message=$("<p>Nothing could be found. You can contact us.</p>");
        $("#profile-container").append(message);
    }
}

//addOne函数
function addOne(data) {
    var html=$("<div class='profile'>\n" +
        "    <!--卡片头-->\n" +
        "    <div class='profile-title'>\n" +
        "        <!--翻译员照片-->\n" +
        "        <div class='col-sm-2'>" +
        "            <a><img class='img-profile' src='' height='140px'></a>" +
        "        </div>\n" +
        "        <!--翻译员简介-->\n" +
        "        <div class='col-sm-10 introduction'>\n" +
        "            <h4 class='introduce-container'>\n" +
        "            </h4>\n" +
        "        </div>\n" +
        "        <div class='clearfix'></div>\n" +
        "    </div>\n" +
        "    <!--卡片体-->\n" +
        "    <div class='profile-body'>\n" +
        "    </div>\n" +
        "</div>");

    var body=html.find(".profile-body");
    profileMessageFrames(2,body);

    writeProfileMessage(data,html);

    $("#profile-container").append(html);
}

//生成一项翻译者表单
function introduce(icon,name,type) {
    return "<div class='introduce row "+type+"'>\n" +
        "    <!--图标和标题-->\n" +
        "    <div class='col-sm-3'>\n" +
        "        <div class='col-sm-2'>\n" +
        "            <span class='glyphicon "+icon+"'></span>\n" +
        "        </div>\n" +
        "        <div class='col-sm-8'>\n" +
        "            <p><strong>"+name+"</strong></p>\n" +
        "        </div>\n" +
        "        <div class='clearfix'></div>\n" +
        "    </div>\n" +
        "    <!--内容块-->\n" +
        "    <div class='col-sm-9 introduce-container'></div>\n" +
        "    <div class='clearfix'></div>\n" +
        "</div>"
}

//写入翻译者数据
function writeProfileMessage(data,parent) {
    //写入数据
    for(var key in data){
        if(key=="image"){
            parent.find(".img-profile").attr("src",data[key]);
        }else if(key=="city"||key=="language"){
            //先清空列表
            parent.find("."+key).find(".introduce-container").text("");
            for(var i=0;i<data[key].length;i++){
                var text=$("<span cid='"+data[key][i].id+"'>"+data[key][i].data+"</span>");
                parent.find("."+key).find(".introduce-container").append(text);
            }
        }else {
            parent.find("."+key).find(".introduce-container").html(data[key].replace(new RegExp("\n","g"),"<br>"));
        }
    }

    //写入跳转
    parent.find("a").attr("href","/html/resume.html?id="+data.id);
}

init();